package nbp.currency.shard;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import nbp.currency.logic.CurrencyType;
import nbp.currency.logic.entity.CurrencyExchange;

public class CurrencyFactory {

	public static CurrencyExchange createCurrencyExchange(CurrencyType type, Date data, BigDecimal bigDecimal) {

		CurrencyExchange currency = new CurrencyExchange();
		currency.setDate(data);
		currency.setAverageExchange(bigDecimal);
		return currency;
	}

	public static CurrencyExchangeDTO createCurrencyExchangeDTO(CurrencyExchange currencyExchange) {

		if (currencyExchange != null) {

			CurrencyExchangeDTO currencyExchangeDTO = new CurrencyExchangeDTO();
			currencyExchangeDTO.setId(currencyExchange.getId());
			currencyExchangeDTO.setDate(currencyExchange.getDate());
			currencyExchangeDTO.setConverter(currencyExchange.getConverter());
			currencyExchangeDTO.setCurrencyType(CurrencyType.getCurrencyById(currencyExchange.getCurrency().getId()));
			currencyExchangeDTO.setAverageExchange(currencyExchange.getAverageExchange());
			return currencyExchangeDTO;

		}
		return null;
	}

	public static CurrencyExchange createCurrencyExchange(CurrencyExchangeDTO currencyExchangeDTO) {
		if (currencyExchangeDTO != null) {
			CurrencyExchange currencyExchange = new CurrencyExchange();
			currencyExchange.setAverageExchange(currencyExchangeDTO.getAverageExchange());
			currencyExchange.setDate(currencyExchangeDTO.getDate());
			currencyExchange.setConverter(currencyExchangeDTO.getConverter());
			if (currencyExchangeDTO.getId() != null) {
				currencyExchange.setId(currencyExchangeDTO.getId());
			}
			return currencyExchange;
		}
		return null;
	}

	public static Collection<CurrencyExchangeDTO> createCurrencyExchangeDTO(Collection<CurrencyExchange> top5Exchange) {
		Collection<CurrencyExchangeDTO> currencyExchangeDTOs = new ArrayList<CurrencyExchangeDTO>();
		for (CurrencyExchange currencyExchange : top5Exchange) {
			CurrencyExchangeDTO createCurrencyExchangeDTO = createCurrencyExchangeDTO(currencyExchange);
			if (createCurrencyExchangeDTO != null) {
				currencyExchangeDTOs.add(createCurrencyExchangeDTO);
			}
		}
		return currencyExchangeDTOs;
	}

}
